package com.exe.test;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.OSSObjectSummary;
import com.aliyun.oss.model.ObjectListing;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;

public class OssUtil {
    // Endpoint以杭州为例，其它Region请按实际情况填写。
    private String endpoint;
    // 阿里云主账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维，请登录RAM控制台创建RAM账号。
    private String accessKeyId;
    private String accessKeySecret;
    // 创建OSSClient实例。
    private OSS ossClient;

    public void init(){
//        endpoint = "https://upload@1090528897958250.onaliyun.com";
        endpoint = "https://oss-cn-beijing.aliyuncs.com";
        accessKeyId="LTAI5t5yi7FmvZTWkiceXEVN";
        accessKeySecret="r7GXRoHDXy6qF3KQEqCK8TLsNIPjiR";

    }
    public void createBucket(String bucketName){
        ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        // 创建存储空间。
        ossClient.createBucket(bucketName);
        // 关闭OSSClient。
        ossClient.shutdown();
    }
    /**
     * <objectName>上传文件到OSS时需要指定包含文件后缀在内的完整路径，例如abc/efg/123.jpg。
      */
    public void upload(String bucketName,String objectName,String content){
        ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        ossClient.putObject(bucketName, objectName, new ByteArrayInputStream(content.getBytes()));
        // 关闭OSSClient。
        ossClient.shutdown();
    }

    public void download(String bucketName,String objectName){
        ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        // 调用ossClient.getObject返回一个OSSObject实例，该实例包含文件内容及文件元信息。
        OSSObject ossObject = ossClient.getObject(bucketName, objectName);
        // 调用ossObject.getObjectContent获取文件输入流，可读取此输入流获取其内容。
        InputStream content = ossObject.getObjectContent();
        if (content != null) {
            BufferedReader reader = new BufferedReader(new InputStreamReader(content));
            try {
                while (true) {
                    String line = reader.readLine();
                    if (line == null) break;
                    System.out.println("\n" + line);
                }
            }catch (Exception e){
                e.printStackTrace();
                try {
                    // 数据读取完成后，获取的流必须关闭，否则会造成连接泄漏，导致请求无连接可用，程序无法正常工作。
                    content.close();
                }catch (Exception e1){
                    e.printStackTrace();
                }
            }

        }
        // 关闭OSSClient。
        ossClient.shutdown();
    }

    public void list(String bucketName,String objectName){
        ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        // ossClient.listObjects返回ObjectListing实例，包含此次listObject请求的返回结果。
        ObjectListing objectListing = ossClient.listObjects(bucketName);
        // objectListing.getObjectSummaries获取所有文件的描述信息。
        for (OSSObjectSummary objectSummary : objectListing.getObjectSummaries()) {
            System.out.println(" - " + objectSummary.getKey() + "  " +
                    "(size = " + objectSummary.getSize() + ")");
        }
        ossClient.shutdown();
    }

    public void delete(String bucketName,String objectName){
        ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        // 删除文件。
        ossClient.deleteObject(bucketName, objectName);
        ossClient.shutdown();
    }

    public static void main(String[] args) {
        OssUtil ossUtil=new OssUtil();
        ossUtil.init();
        //ossUtil.upload("exewebupload","test/test.txt","test");
        //ossUtil.download("exewebupload","test/test.txt");
        ossUtil.list("exewebupload","test/test.txt");
    }

}
